//
//  HQAddModuleViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/11.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQAddModuleViewController.h"

@interface HQAddModuleViewController ()<ZHPickViewDelegate>
{
    NSDictionary *_dataInfo;
}
@end

@implementation HQAddModuleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置样式组件";
    
    [self requestSeyle];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQAddModuleCell *cell = [HQAddModuleCell cellWithTableView:tableView];
    cell.dataInfo= _dataSource[indexPath.row];
    [cell.deleteItem addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
    [cell.editItem addTarget:self action:@selector(editItems:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

/**
 删除组件
 
 @param button 按钮
 */
-(void)deleteItem:(HQButton *)button{
    [HQNetWork requetParam:[HQParam deleteComponents:[UserModelManager userID] withCId:button.dataInfo[@"c_id"]]  withMethod:[HQMethod delCom] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"组件删除成功"];
            [self requestSeyle];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 编辑组件
 
 @param button 按钮
 */
-(void)editItems:(HQButton *)button{
    [_cName becomeFirstResponder];
    _dataInfo = button.dataInfo;
    _cName.text = button.dataInfo[@"c_name"];
    _cUnit.text = button.dataInfo[@"c_unit"];
    [_addOrEditItem setTitle:@"编辑" forState:UIControlStateNormal];
}

/**
 请求样式列表
 */
-(void)requestSeyle{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam allComponents:[UserModelManager userID]] withMethod:[HQMethod compontIndex] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dataSource.count == 0){
                [self addNoneDataView:_tableView withText:@"请您在下方输入 \r\r 组件名称和选择单位名称，进行添加"];
            }
            [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择单位
 
 @param sender 按钮
 */
- (IBAction)selectDanWeiItem:(id)sender {
    [self.view endEditing:YES];
    ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:@[@"米",@"个"] isHaveNavControler:NO];
    [_zhPickerView setPickeTitle:@"选择单位"];
    [_zhPickerView selectObj:_cName.text];
    _zhPickerView.delegate = self;
    _zhPickerView.tag = 1001;
    [_zhPickerView show];
}

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    _cUnit.text = resultString;
}

- (IBAction)addOrEditItem:(id)sender {
    if([_addOrEditItem.titleLabel.text isEqualToString:@"编辑"]){
        [self.view endEditing:YES];
        if([self isInputEmpty:_cName.text]){
            [self tostView:@"请输入组件名称"];
            return;
        }
        if([self isInputEmpty:_cUnit.text]){
            [self tostView:@"请选择单位"];
            return;
        }
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam editComponents:_dataInfo[@"c_id"] withCname:_cName.text withCUnit:_cUnit.text]  withMethod:[HQMethod editCom] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                [self tostView:@"组件修改成功"];
                _cName.text = @"";
                _cUnit.text = @"";
                [_addOrEditItem setTitle:@"新增" forState:UIControlStateNormal];
                [self requestSeyle];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }else{
        [self.view endEditing:YES];
        if([self isInputEmpty:_cName.text]){
            [self tostView:@"请输入组件名称"];
            return;
        }
        if([self isInputEmpty:_cUnit.text]){
            [self tostView:@"请选择单位"];
            return;
        }
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam addComponents:[UserModelManager userID] withCname:_cName.text withCUnit:_cUnit.text]  withMethod:[HQMethod addCom] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                [self tostView:@"添加组件成功"];
                _cName.text = @"";
                _cUnit.text = @"";
                [self requestSeyle];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }
}

@end
